﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Netduino4Fun.WebServer.Test.Helper;
using Netduino4Fun.WebServer.WebServer.Api;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using ws = Netduino4Fun.WebServer;

namespace Netduino4Fun.WebServer.Test
{
	public class Program
	{
		public static RGBLed Led;

		public static void Main()
		{
			Led = new RGBLed(Pins.GPIO_PIN_D10, Pins.GPIO_PIN_D9, Pins.GPIO_PIN_D6);
			// Instantiate a new web server on port 80.
			var server = new HTTPServer(80);
			server.WebRequestHandler += server_WebRequestHandler;

			// Infinite sleep
			Thread.Sleep(-1);
		}

		public static void RgbToLed(uint r, uint g, uint b)
		{
			Debug.Print(r + " " + g + " " + " " + b);
			Led.ChangeColor(r,g,b);
		}

		public static string GetPostValue(RequestParameter[] POST, string key)
		{
			foreach (var p in POST)
			{
				if (p.Field == key)
					return p.Value;
			}
			return string.Empty;
		}

		static void server_WebRequestHandler(RequestHandler requestHandler)
		{

			if ((requestHandler.URL == "/favicon.ico") & (requestHandler.Method == "GET"))
			{
				requestHandler.Status = "HTTP/1.0 200 OK";
				HTTPHeader.Set(requestHandler.ResponseHeaders, "Content-Type", "image/png");
				HTTPHeader.Set(requestHandler.ResponseHeaders, "Cache-Control", "no-cache");
				requestHandler.ResponseBody = Resources.GetBytes(Resources.BinaryResources.iconLed1);
			}

			if ((requestHandler.URL == "/led") & (requestHandler.Method == "GET"))
			{
				requestHandler.Status = "HTTP/1.0 200 OK";
				HTTPHeader.Set(requestHandler.ResponseHeaders, "Content-Type", "text/html");
				HTTPHeader.Set(requestHandler.ResponseHeaders, "Cache-Control", "no-cache");
				requestHandler.ResponseBody = Resources.GetBytes(Resources.BinaryResources.led);
			}

			if ((requestHandler.URL == "/led/setRGB") & (requestHandler.Method == "POST"))
			{

				requestHandler.Status = "HTTP/1.0 200 OK";
				HTTPHeader.Set(requestHandler.ResponseHeaders, "Content-Type", "text/html");
				HTTPHeader.Set(requestHandler.ResponseHeaders, "Cache-Control", "no-cache");
				try
				{
					if (requestHandler.POST != null)
					{
						var r = GetPostValue(requestHandler.POST, "r");
						var g = GetPostValue(requestHandler.POST, "g");
						var b = GetPostValue(requestHandler.POST, "b");
						RgbToLed(r.ToUint(), g.ToUint(), b.ToUint());	
					}
				}
				catch (Exception ex)
				{
					Debug.Print(ex.Message);
				}
				requestHandler.ResponseBody = Encoding.UTF8.GetBytes("Color changed");
			}

			if ((requestHandler.URL == "/led/enable") & (requestHandler.Method == "POST"))
			{

				requestHandler.Status = "HTTP/1.0 200 OK";
				HTTPHeader.Set(requestHandler.ResponseHeaders, "Content-Type", "text/html");
				HTTPHeader.Set(requestHandler.ResponseHeaders, "Cache-Control", "no-cache");
				try
				{
					if (requestHandler.POST != null)
					{
						var val = GetPostValue(requestHandler.POST, "val");
						if(val == "1")
							Led.Start();
						else
							Led.Stop();

					}
				}
				catch (Exception ex)
				{
					Debug.Print(ex.Message);
				}
				requestHandler.ResponseBody = Encoding.UTF8.GetBytes("Color changed");
			}

			//// Load colorpicker
			//if (requestHandler.URL == "/colorpicker.js")
			//{
			//    requestHandler.Status = "HTTP/1.0 200 OK";
			//    HTTPHeader.Set(requestHandler.ResponseHeaders, "Content-Type", "text/javascript");
			//    requestHandler.ResponseBody = Resources.GetBytes(Resources.BinaryResources.colorpicker);
			//}

			//// Load colorwheel
			//if (requestHandler.URL == "/colorwheel.js")
			//{
			//    requestHandler.Status = "HTTP/1.0 200 OK";
			//    HTTPHeader.Set(requestHandler.ResponseHeaders, "Content-Type", "text/javascript");
			//    requestHandler.ResponseBody = Resources.GetBytes(Resources.BinaryResources.colorwheel);
			//}

			//// Load jquery
			//if (requestHandler.URL == "/jquery-1.7.1.min.js")
			//{
			//    requestHandler.Status = "HTTP/1.0 200 OK";
			//    HTTPHeader.Set(requestHandler.ResponseHeaders, "Content-Type", "text/javascript");
			//    requestHandler.ResponseBody = Resources.GetBytes(Resources.BinaryResources.jquery);
			//}

			//// Load jquery mobile
			//if (requestHandler.URL == "/jquery.mobile-1.0.1.min.js")
			//{
			//    requestHandler.Status = "HTTP/1.0 200 OK";
			//    HTTPHeader.Set(requestHandler.ResponseHeaders, "Content-Type", "text/javascript");
			//    requestHandler.ResponseBody = Resources.GetBytes(Resources.BinaryResources.jquery_mobile);
			//}
		}

		//static void server_WebRequestHandler(ws.Api.RequestHandler requestHandler)
		//{
		//    // Send logo
		//    if ((requestHandler.URL == "/logo.png") & (requestHandler.Method == "GET"))
		//    {
		//        requestHandler.Status = "HTTP/1.0 200 OK";
		//        HTTPHeader.Set(requestHandler.ResponseHeaders, "Content-Type", "image/png");
		//        HTTPHeader.Set(requestHandler.ResponseHeaders, "Cache-Control", "no-cache");
		//        requestHandler.ResponseBody = Resources.GetBytes(Resources.BinaryResources.logo);
		//    }

		//    // Sample Ajax request
		//    if ((requestHandler.URL == "/ajax.txt") & (requestHandler.Method == "GET"))
		//    {
		//        requestHandler.Status = "HTTP/1.0 200 OK";
		//        HTTPHeader.Set(requestHandler.ResponseHeaders, "Content-Type", "text/plain");
		//        HTTPHeader.Set(requestHandler.ResponseHeaders, "Cache-Control", "no-cache");
		//        requestHandler.ResponseBody = Encoding.UTF8.GetBytes(RequestCount.ToString() + "th request");
		//        RequestCount++;
		//    }

		//    // Load prototype
		//    if (requestHandler.URL == "/prototype.js")
		//    {
		//        requestHandler.Status = "HTTP/1.0 200 OK";
		//        HTTPHeader.Set(requestHandler.ResponseHeaders, "Content-Type", "text/javascript");
		//        requestHandler.ResponseBody = Resources.GetBytes(Resources.BinaryResources.prototype);
		//    }

		//    // Main page with form and post result
		//    if (requestHandler.URL == "/")
		//    {
		//        if (requestHandler.Method == "GET")
		//        {
		//            // Set Status Ok
		//            requestHandler.Status = "HTTP/1.0 200 OK";

		//            // Set correct content type
		//            HTTPHeader.Set(requestHandler.ResponseHeaders, "Content-Type", "text/html");

		//            // Get Form
		//            requestHandler.ResponseBody = Resources.GetBytes(Resources.BinaryResources.posttest);
		//        }

		//        if (requestHandler.Method == "POST")
		//        {
		//            // Set Status Ok
		//            requestHandler.Status = "HTTP/1.0 200 OK";

		//            // Set correct content type
		//            HTTPHeader.Set(requestHandler.ResponseHeaders, "Content-Type", "text/plain");

		//            // Output request headers
		//            String ResponseBodyTxt = "";
		//            ResponseBodyTxt = "Request header: \r\n";
		//            ResponseBodyTxt += HTTPHeader.OutputResponseHeader(requestHandler.RequestHeaders);
		//            ResponseBodyTxt += "\r\n";

		//            // Output post values
		//            ResponseBodyTxt += "Value 1: \r\n";
		//            ResponseBodyTxt += RequestParameter.Get(requestHandler.POST, "value1", true) + "\r\n";
		//            ResponseBodyTxt += "Value 2: \r\n";
		//            ResponseBodyTxt += RequestParameter.Get(requestHandler.POST, "value2", true) + "\r\n";

		//            // Convert to byte[] array
		//            requestHandler.ResponseBody = Encoding.UTF8.GetBytes(ResponseBodyTxt);
		//        }
		//    }
		//}

	}
}
